package com.gakj.truck.third.core.utils;

import com.gakj.truck.third.core.enums.EnumUserType;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;


/**
 * @ClassName: SMSSender
 * @Description: TODO 发送 短信
 * @author henryfang1@163.com
 * @date 2012-9-9 下午03:51:34
 * 
 */

public class SMSDYSender {
    private static Logger logger = LoggerFactory.getLogger(SMSDYSender.class);

	// 发送传递的参数
	private static String url="http://gw.api.taobao.com/router/rest";
	private static String AppKey ="23319617";
	private static String AppSecret="e64a5820f13caf3c1c2f81afe17d5abf";


	/**
	 * 发送短信
	 * 
	 * @author zhaozhe
	 * @time 2015年10月18日
	 * @param mobile
	 *            手机号
	 * @param content
	 *            内容
	 *            短信类型，通知/验证码
	 * @return
	 */
	public static void sendSms(String mobile, String content){
		logger.info("手机号："+mobile+"调用大鱼api,验证码为："+content);
		String json="{\"number\":'"+content+"'}";  
		TaobaoClient client = new DefaultTaobaoClient(url, AppKey, AppSecret);  
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();  
		req.setExtend("123456");  
		req.setSmsType("normal");  
		req.setSmsFreeSignName("蜗牛货车");  
		req.setSmsParamString(json);  
		req.setRecNum(mobile);  
		req.setSmsTemplateCode("SMS_10680541");   
		try {  
		    AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req); 
			Object times = RedisClient
					.cacheGet(mobile.concat("_code_times_").concat(DateUtil.formatDateToString(new Date(), "yyyyMMdd")));
		    RedisClient.cacheAdd(
					mobile.concat("_code_times_").concat(DateUtil.formatDateToString(new Date(), "yyyyMMdd")),
					times.toString(), 60 * 60 * 24);
		    System.out.println(rsp.getBody());    
		    logger.info(rsp.getBody());
		} catch (Exception e) {  
		    // TODO: handle exception  
		}  
		
	}
	
	/**
	 * 用户注册短信发送
	 * @author zhaozhe
	 * @time 2015年10月18日
	 */
	public static void sendSms(String servicetel){
		logger.info("客服电话：："+servicetel+"调用大鱼api");
		String json="{\"number\":"+ EnumUserType.SERVICEHOTLINE.getName()+"}";
		TaobaoClient client = new DefaultTaobaoClient(url, AppKey, AppSecret);  
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();  
		req.setExtend("123456");  
		req.setSmsType("normal");  
		req.setSmsFreeSignName("蜗牛货车");  
		req.setSmsParamString(json);  
		req.setRecNum(servicetel);  
		req.setSmsTemplateCode("SMS_7290080");  
		try {  
		    AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req); 
		    System.out.println(rsp.getBody());    
		    logger.info(rsp.getBody());
		} catch (Exception e) {  
		    // TODO: handle exception  
		}  
		
	}
	
	public static void main(String[] args) {
		sendSms("18611743529","6666");
	}
}